<?php include("../../includes/includes.php"); ?>
<?
connect($config);
$players = getPlayerReplacementNames();
$textDump = $_POST['textDump'];
$lines = preg_split("%\n%", $textDump);
$count = count($lines);
$round = getCurrentRound();
for ($i = 0; $i < $count; $i++) {
	$lineCells = preg_split("%\t%", $lines[$i]);
	if (count($lineCells) == 1) {
		$team = trim($lineCells[0]);
	}
	else {
		$spaceIndex = strpos($lineCells[0], " ", 0);
	    $firstName = trim(substr($lineCells[0], 0, $spaceIndex));
	    $surname = trim(substr($lineCells[0], $spaceIndex + 1));
	    $score = trim($lineCells[1]);
	    
	    $count2 = count($players);
	    for ($j = 0; $j < $count2; $j++) {
	    	if ($players[$j]->firstName == $firstName && $players[$j]->surname == $surname && $players[$j]->team == $team) {
	    		$firstName = $players[$j]->requiredFirstName;
	    		$surname = $players[$j]->requiredSurname;
	    		
	    		break;
	    	}
	    }
	    
	    $playerId = getPlayerID($firstName, $surname, $team);
	    if ($playerId == null) {
	    	echo "<tr><td>ERROR: PLAYER NOT FOUND:" . $firstName . " " . $surname . " (" . $team . ")</td></tr>"; 
	    }
	    else {
	    	$hasScoureForRound = hasPlayerScoreAlreadyForRound($round, $playerId);
			if (!$hasScoureForRound) {
				//echo "INSERT INTO playerscore (round, playerId, score) VALUES (" . $round . "," . $playerId . "," . $score . ");<br>";
				addPlayerScore($round, $playerId, $score);
			}
	    }
	}
}

createDisplayRound();
createFixture();
mysql_close();
echo "</table><br><b>COMPLETED!!</b>";
?>